home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / zines / Phrack / Phrack Issue 53.sit / 53 / P53-10 < prev    next >
Text File  |  1998-07-08  |  22KB  |  995 lines

  1. ---[  Phrack Magazine   Volume 8, Issue 53 July 8, 1998, article 10 of 15
  2.  
  3.  
  4. -------------------------[  Interface Promiscuity Obscurity
  5.  
  6.  
  7. --------[  apk <apk@itl.waw.pl>
  8.  
  9.  
  10.  
  11. ----[  INTRODUCTION
  12.  
  13. Normally, when you put an interface into promiscuous mode, it sets a flag
  14. in the device interface structure telling the world (or anyone who wants
  15. to check) that the device, is indeed, in promiscuous mode.  This is, of
  16. course, annoying to those of you who want to obscure this fact from prying
  17. administrative eyes.  Behold intrepid hacker, your salvation is at hand.
  18. The following modules for FreeBSD, Linux, HP-UX, IRIX and Solaris allow you
  19. to obscure the IFF_PROMISC bit and run all your wonderful little packet
  20. sniffers incognito...
  21.  
  22.  
  23. ----[  IMPLEMENTATION DETAILS
  24.  
  25. Usage of the code is simple.  After you put the interface into promiscuous
  26. mode, you can clean the IFF_PROMISC flag with:
  27.  
  28.     `./i <interface> 0`
  29.  
  30. and reset the flag with:
  31.  
  32.     `./i <interface> 1`.
  33.  
  34. Note that these programs only change interface's flag value, they don't affect
  35. NIC status.  On systems which allow setting promiscuous mode by SIOCSIFFLAGS
  36. however, any call to SIOCSIFFLAGS will make the change take effect (e.g. after
  37. clearing promisc flag:
  38.  
  39.     'ifconfig <interface> up'
  40.  
  41. will really turn off promiscuous mode).  Systems for which above is true are:
  42. FreeBSD, Linux, Irix.  On these three you can run a sniffer in non-promiscuous
  43. mode, and then some time later set IFF_PROIMISC on the interface, then with
  44. the above command set promiscuous mode for interface.  This is most useful on
  45. FreeBSD because in doing this you won't get that annoying `promiscuous mode
  46. enabled for <interface>' message in the dmesg buffer (it's only logged when
  47. you enable promiscuous mode via bpf by BIOCPROMISC).
  48.  
  49. On Solaris, every alias has its own flags, so you can set flags for any alias:
  50.  
  51.     'interface[:<alias number>]'
  52.  
  53. (because Solaris doesn't set IFF_PROMISC when you turn on promiscuous mode
  54. using DLPI you don't need this program however).
  55.  
  56.  
  57. ----[  THE CODE
  58.  
  59. <++> EX/promisc/freebsd-p.c
  60. /*
  61.  *  promiscuous flag changer v0.1, apk
  62.  *  FreeBSD version, compile with -lkvm
  63.  *
  64.  *  usage: promisc [interface 0|1]
  65.  *
  66.  *  note:  look at README for notes
  67.  */
  68.  
  69. #ifdef __FreeBSD__
  70. # include <osreldate.h>
  71. # if __FreeBSD_version >= 300000
  72. #  define FBSD3
  73. # endif
  74. #endif
  75.  
  76. #include <sys/types.h>
  77. #include <sys/time.h>
  78.  
  79. #include <sys/socket.h>
  80. #include <net/if.h>
  81. #ifdef FBSD3
  82. # include <net/if_var.h>
  83. #endif
  84.  
  85. #include <kvm.h>
  86. #include <nlist.h>
  87.  
  88. #include <stdio.h>
  89. #include <stdlib.h>
  90. #include <errno.h>
  91. #include <string.h>
  92. #include <fcntl.h>
  93. #include <unistd.h>
  94.  
  95. #define IFFBITS \
  96. "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING" \
  97. "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
  98. "\20MULTICAST"
  99.  
  100. struct nlist nl[] = {
  101.     { "_ifnet" },
  102. #define N_IFNET 0
  103.     { "" }
  104. };
  105.  
  106. int kread(kvm_t *kd, u_long addr, void *buf, int len) {
  107.   int c;
  108.  
  109.   if ((c = kvm_read(kd, addr, buf, len)) != len) 
  110.     return -1;
  111.   return c;
  112. }
  113.  
  114. int kwrite(kvm_t *kd, u_long addr, void *buf, int len) {
  115.   int c;
  116.  
  117.   if ((c = kvm_write(kd, addr, buf, len)) != len) 
  118.     return -1;
  119.   return c;
  120. }
  121.  
  122. void usage(char *s) {
  123.   printf("usage: %s [interface 0|1]\n", s);
  124.   exit(1);
  125. }
  126.  
  127. int main(int argc, char *argv[]) {
  128. #ifdef FBSD3
  129.   struct ifnethead ifh;
  130. #endif
  131.   struct ifnet ifn, *ifp;
  132.   char ifname[IFNAMSIZ];
  133.   int unit, promisc, i, any;
  134.   char *interface, *cp;
  135.   kvm_t *kd;
  136.  
  137.   switch (argc) {
  138.     case 1:
  139.     promisc = -1;
  140.     interface = NULL;
  141.     break;
  142.     case 3:
  143.         interface = argv[1];
  144.         if ((cp = strpbrk(interface, "1234567890")) == NULL) {
  145.             printf("bad interface name: %s\n", interface);
  146.           exit(1);
  147.         }
  148.         unit = strtol(cp, NULL, 10);
  149.         *cp = 0;
  150.     promisc = atoi(argv[2]);
  151.     break;
  152.     default:
  153.     usage(argv[0]);
  154.   }
  155.  
  156.   if ((kd = kvm_open(NULL, NULL, NULL, O_RDWR, argv[0])) == NULL) 
  157.     exit(1);
  158.  
  159.   if (kvm_nlist(kd, nl) == -1) {
  160.     perror("kvm_nlist");
  161.     exit(1);
  162.   }
  163.  
  164.   if (nl[N_IFNET].n_type == 0) {
  165.     printf("Cannot find symbol: %s\n", nl[N_IFNET].n_name);
  166.     exit(1);
  167.   }
  168.  
  169. #ifdef FBSD3
  170.   if (kread(kd, nl[N_IFNET].n_value, &ifh, sizeof(ifh)) == -1) {
  171.     perror("kread");
  172.     exit(1);
  173.   }
  174.   ifp = ifh.tqh_first;
  175. #else
  176.   if (kread(kd, nl[N_IFNET].n_value, &ifp, sizeof(ifp)) == -1) {
  177.     perror("kread");
  178.     exit(1);
  179.   }
  180.   if (kread(kd, (u_long)ifp, &ifp, sizeof(ifp)) == -1) {
  181.     perror("kread");
  182.     exit(1);
  183.   }
  184. #endif
  185.  
  186. #ifdef FBSD3
  187.   for (; ifp; ifp = ifn.if_link.tqe_next) {
  188. #else
  189.   for (; ifp; ifp = ifn.if_next) {
  190. #endif
  191.     if (kread(kd, (u_long)ifp, &ifn, sizeof(ifn)) == -1) {
  192.       perror("kread");
  193.       break;
  194.     }
  195.     if (kread(kd, (u_long)ifn.if_name, ifname, sizeof(ifname)) == -1) {
  196.       perror("kread");
  197.       break;
  198.     }
  199.     printf("%d: %s%d, flags=0x%x ", ifn.if_index, ifname, ifn.if_unit, 
  200.        (unsigned short)ifn.if_flags);
  201.     /* this is from ifconfig sources */
  202.     cp = IFFBITS;
  203.     any = 0;
  204.     putchar('<');
  205.     while ((i = *cp++) != 0) {
  206.       if (ifn.if_flags & (1 << (i-1))) {
  207.     if (any)
  208.       putchar(',');
  209.     any = 1;
  210.     for (; *cp > 32; )
  211.       putchar(*cp++);
  212.       } else 
  213.     for (; *cp > 32; cp++)
  214.       ;
  215.     }
  216.     putchar('>');
  217.     putchar('\n');
  218.     if (interface && strcmp(interface, ifname) == 0 && unit == ifn.if_unit) {
  219.       switch (promisc) {
  220.     case -1: 
  221.         break;
  222.         case 0: if ((ifn.if_flags & IFF_PROMISC) == 0)
  223.           printf("\tIFF_PROMISC not set\n");
  224.                 else {
  225.           printf("\t%s%d: clearing IFF_PROMISC\n", ifname, unit);
  226.           ifn.if_flags &= ~IFF_PROMISC;
  227.                 if (kwrite(kd, (u_long)ifp, &ifn, sizeof(ifn)) == -1) 
  228.                     perror("kwrite");
  229.                 }
  230.           break;
  231.     default: if ((ifn.if_flags & IFF_PROMISC) == IFF_PROMISC)
  232.           printf("\tIFF_PROMISC set already\n");
  233.                 else {
  234.               printf("\t%s%d: setting IFF_PROMISC\n", ifname, unit);
  235.           ifn.if_flags |= IFF_PROMISC;
  236.                 if (kwrite(kd, (u_long)ifp, &ifn, sizeof(ifn)) == -1) 
  237.                     perror("kwrite");
  238.                 }
  239.                 break;
  240.  
  241.       }
  242.     }
  243.   }
  244. }
  245. <-->
  246. <++> EX/promisc/hpux-p.c
  247. /*
  248.  *  promiscuous flag changer v0.1, apk
  249.  *  HP-UX version, on HP-UX 9.x compile with -DHPUX9
  250.  *
  251.  *  usage: promisc [interface 0|1]
  252.  *
  253.  *  note:  look at README for notes
  254.  */
  255.  
  256. /* #define HPUX9 on HP-UX 9.x */
  257.  
  258. #include <sys/types.h>
  259. #include <sys/socket.h>
  260.  
  261. #include <net/if.h>
  262.  
  263. #include <nlist.h>
  264. #include <stdio.h>
  265. #include <stdlib.h>
  266. #include <errno.h>
  267. #include <string.h>
  268. #include <fcntl.h>
  269. #include <unistd.h>
  270.  
  271. #ifndef HPUX9
  272. # define PATH_VMUNIX "/stand/vmunix"
  273. #else
  274. # define PATH_VMUNIX "/hp-ux"
  275. #endif
  276.  
  277. #define PATH_KMEM "/dev/kmem"
  278. #define IFFBITS \
  279. "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING" \
  280. "\10NOARP\11PROMISC\12ALLMULTI\13LOCALSUBNETS\14MULTICAST\15CKO\16xNOACC"
  281.  
  282. struct nlist nl[] = {
  283.     { "ifnet" },
  284. #define N_IFNET 0
  285.     { "" }
  286. };
  287.  
  288. int kread(fd, addr, buf, len) 
  289. int fd, len;
  290. off_t addr;
  291. void *buf;
  292. {
  293.   int c;
  294.  
  295.   if (lseek(fd, addr, SEEK_SET) == -1) 
  296.     return -1;
  297.   if ((c = read(fd, buf, len)) != len) 
  298.     return -1;
  299.   return c;
  300. }
  301.  
  302. int kwrite(fd, addr, buf, len) 
  303. int fd, len;
  304. off_t addr;
  305. void *buf;
  306. {
  307.   int c;
  308.  
  309.   if (lseek(fd, addr, SEEK_SET) == -1) 
  310.     return -1;
  311.   if ((c = write(fd, buf, len)) != len) 
  312.     return -1;
  313.   return c;
  314. }
  315.  
  316. void usage(s) 
  317. char *s;
  318. {
  319.   printf("usage: %s [interface 0|1]\n", s);
  320.   exit(1);
  321. }
  322.  
  323. main(argc, argv) 
  324. int argc;
  325. char **argv;
  326. {
  327.   struct ifnet ifn, *ifp;
  328.   char ifname[IFNAMSIZ];
  329.   int fd, unit, promisc, i, any;
  330.   char *interface, *cp;
  331.  
  332.   switch (argc) {
  333.     case 1:
  334.     promisc = -1;
  335.     interface = NULL;
  336.     break;
  337.     case 3:
  338.         interface = argv[1];
  339.         if ((cp = strpbrk(interface, "1234567890")) == NULL) {
  340.             printf("bad interface name: %s\n", interface);
  341.           exit(1);
  342.         }
  343.         unit = strtol(cp, NULL, 10);
  344.         *cp = 0;
  345.     promisc = atoi(argv[2]);
  346.     break;
  347.     default:
  348.     usage(argv[0]);
  349.   }
  350.  
  351.   if (nlist(PATH_VMUNIX, nl) == -1) {
  352.     perror(PATH_VMUNIX);
  353.     exit(1);
  354.   }
  355.   if (nl[N_IFNET].n_type == 0) {
  356.     printf("Cannot find symbol: %s\n", nl[0].n_name);
  357.     exit(1);
  358.   }
  359.  
  360.   if ((fd = open(PATH_KMEM, O_RDWR)) == -1) {
  361.     perror(PATH_KMEM);
  362.     exit(1);
  363.   }
  364.   if (kread(fd, nl[N_IFNET].n_value, &ifp, sizeof(ifp)) == -1) {
  365.     perror("kread");
  366.     exit(1);
  367.   }
  368.  
  369.   for (; ifp; ifp = ifn.if_next) {
  370.     if (kread(fd, (u_long)ifp, &ifn, sizeof(ifn)) == -1) {
  371.       perror("kread");
  372.       break;
  373.     }
  374.     if (kread(fd, (u_long)ifn.if_name, ifname, sizeof(ifname)) == -1) {
  375.       perror("kread");
  376.       break;
  377.     }
  378.     printf("%d: %s%d, flags=0x%x ", ifn.if_index, ifname, ifn.if_unit, 
  379.        ifn.if_flags);
  380.     cp = IFFBITS;
  381.     any = 0;
  382.     putchar('<');
  383.     while ((i = *cp++) != 0) {
  384.       if (ifn.if_flags & (1 << (i-1))) {
  385.     if (any)
  386.       putchar(',');
  387.     any = 1;
  388.     for (; *cp > 32; )
  389.       putchar(*cp++);
  390.       } else 
  391.     for (; *cp > 32; cp++)
  392.       ;
  393.     }
  394.     putchar('>');
  395.     putchar('\n');
  396.     if (interface && strcmp(interface, ifname) == 0 && unit == ifn.if_unit) {
  397.       switch (promisc) {
  398.     case -1: 
  399.         break;
  400.         case 0: if ((ifn.if_flags & IFF_PROMISC) == 0)
  401.           printf("\tIFF_PROMISC not set\n");
  402.                 else {
  403.           printf("\t%s%d: clearing IFF_PROMISC\n", ifname, unit);
  404.           ifn.if_flags &= ~IFF_PROMISC;
  405.                 if (kwrite(fd, (u_long)ifp, &ifn, sizeof(ifn)) == -1) 
  406.                     break;
  407.                 }
  408.           break;
  409.     default: if ((ifn.if_flags & IFF_PROMISC) == IFF_PROMISC)
  410.           printf("\tIFF_PROMISC set already\n");
  411.                 else {
  412.               printf("\t%s%d: setting IFF_PROMISC\n", ifname, unit);
  413.           ifn.if_flags |= IFF_PROMISC;
  414.                 if (kwrite(fd, (u_long)ifp, &ifn, sizeof(ifn)) == -1) 
  415.                     break;
  416.                 }
  417.  
  418.       }
  419.     }
  420.   }
  421. }
  422. <-->
  423. <++> EX/promisc/irix-p.c
  424. /*
  425.  *  promiscuous flag changer v0.1, apk
  426.  *  Irix version, on Irix 6.x compile with -lelf, on 5.x with -lmld
  427.  *
  428.  *  usage: promisc [interface 0|1]
  429.  *
  430.  *  note: look at README for notes on irix64 compile with -DI64 -64
  431.  */
  432.  
  433. /* #define I64 for Irix64*/
  434.  
  435. #include <sys/types.h>
  436. #include <sys/socket.h>
  437.  
  438. #include <net/if.h>
  439.  
  440. #include <nlist.h>
  441. #include <stdio.h>
  442. #include <stdlib.h>
  443. #include <errno.h>
  444. #include <string.h>
  445. #include <fcntl.h>
  446. #include <unistd.h>
  447.  
  448. #define PATH_VMUNIX "/unix"
  449.  
  450. #define PATH_KMEM "/dev/kmem"
  451. #define IFFBITS \
  452. "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING" \
  453. "\10NOARP\11PROMISC\12ALLMULTI\13LOCALSUBNETS\14MULTICAST\15CKO\16xNOACC"
  454.  
  455. #ifdef I64
  456. struct nlist64 nl[] = {
  457. #else
  458. struct nlist nl[] = {
  459. #endif
  460.     { "ifnet" },
  461. #define N_IFNET 0
  462.     { "" }
  463. };
  464.  
  465. int kread(int fd, off_t addr, void *buf, int len) {
  466.   int c;
  467.  
  468. #ifdef I64
  469.   if (lseek64(fd, (off_t)addr, SEEK_SET) == -1) 
  470. #else
  471.   if (lseek(fd, (off_t)addr, SEEK_SET) == -1) 
  472. #endif
  473.     return -1;
  474.   if ((c = read(fd, buf, len)) != len) 
  475.     return -1;
  476.   return c;
  477. }
  478.  
  479. int kwrite(int fd, off_t addr, void *buf, int len) {
  480.   int c;
  481.  
  482. #ifdef I64
  483.   if (lseek64(fd, (off_t)addr, SEEK_SET) == -1) 
  484. #else
  485.   if (lseek(fd, (off_t)addr, SEEK_SET) == -1) 
  486. #endif
  487.     return -1;
  488.   if ((c = write(fd, buf, len)) != len) 
  489.     return -1;
  490.   return c;
  491. }
  492.  
  493. void usage(s) 
  494. char *s;
  495. {
  496.   printf("usage: %s [interface 0|1]\n", s);
  497.   exit(1);
  498. }
  499.  
  500. main(argc, argv) 
  501. int argc;
  502. char **argv;
  503. {
  504.   struct ifnet ifn, *ifp;
  505.   char ifname[IFNAMSIZ];
  506.   int fd, unit, promisc, i, any;
  507.   char *interface, *cp;
  508.  
  509.   switch (argc) {
  510.     case 1:
  511.     promisc = -1;
  512.     interface = NULL;
  513.     break;
  514.     case 3:
  515.         interface = argv[1];
  516.         if ((cp = strpbrk(interface, "1234567890")) == NULL) {
  517.             printf("bad interface name: %s\n", interface);
  518.           exit(1);
  519.         }
  520.         unit = strtol(cp, NULL, 10);
  521.         *cp = 0;
  522.     promisc = atoi(argv[2]);
  523.     break;
  524.     default:
  525.     usage(argv[0]);
  526.   }
  527.  
  528. #ifdef I64
  529.   if (nlist64(PATH_VMUNIX, nl) == -1) {
  530. #else
  531.   if (nlist(PATH_VMUNIX, nl) == -1) {
  532. #endif
  533.     perror(PATH_VMUNIX);
  534.     exit(1);
  535.   }
  536.   if (nl[N_IFNET].n_type == 0) {
  537.     printf("Cannot find symbol: %s\n", nl[0].n_name);
  538.     exit(1);
  539.   }
  540.  
  541.   if ((fd = open(PATH_KMEM, O_RDWR)) == -1) {
  542.     perror(PATH_KMEM);
  543.     exit(1);
  544.   }
  545.   if (kread(fd, nl[N_IFNET].n_value, &ifp, sizeof(ifp)) == -1) {
  546.     perror("kread");
  547.     exit(1);
  548.   }
  549.  
  550.   for (; ifp; ifp = ifn.if_next) {
  551.     if (kread(fd, (u_long)ifp, &ifn, sizeof(ifn)) == -1) {
  552.       perror("kread");
  553.       break;
  554.     }
  555.     if (kread(fd, (u_long)ifn.if_name, ifname, sizeof(ifname)) == -1) {
  556.       perror("kread");
  557.       break;
  558.     }
  559.     printf("%d: %s%d, flags=0x%x ", ifn.if_index, ifname, ifn.if_unit, 
  560.        ifn.if_flags);
  561.     cp = IFFBITS;
  562.     any = 0;
  563.     putchar('<');
  564.     while ((i = *cp++) != 0) {
  565.       if (ifn.if_flags & (1 << (i-1))) {
  566.     if (any)
  567.       putchar(',');
  568.     any = 1;
  569.     for (; *cp > 32; )
  570.       putchar(*cp++);
  571.       } else 
  572.     for (; *cp > 32; cp++)
  573.       ;
  574.     }
  575.     putchar('>');
  576.     putchar('\n');
  577.     if (interface && strcmp(interface, ifname) == 0 && unit == ifn.if_unit) {
  578.       switch (promisc) {
  579.     case -1: 
  580.         break;
  581.         case 0: if ((ifn.if_flags & IFF_PROMISC) == 0)
  582.           printf("\tIFF_PROMISC not set\n");
  583.                 else {
  584.           printf("\t%s%d: clearing IFF_PROMISC\n", ifname, unit);
  585.           ifn.if_flags &= ~IFF_PROMISC;
  586.                 if (kwrite(fd, (u_long)ifp, &ifn, sizeof(ifn)) == -1) 
  587.                     break;
  588.                 }
  589.           break;
  590.     default: if ((ifn.if_flags & IFF_PROMISC) == IFF_PROMISC)
  591.           printf("\tIFF_PROMISC set already\n");
  592.                 else {
  593.               printf("\t%s%d: setting IFF_PROMISC\n", ifname, unit);
  594.           ifn.if_flags |= IFF_PROMISC;
  595.                 if (kwrite(fd, (u_long)ifp, &ifn, sizeof(ifn)) == -1) 
  596.                     break;
  597.                 }
  598.  
  599.       }
  600.     }
  601.   }
  602. }
  603. <-->
  604. <++> EX/promisc/linux-p.c
  605. /*
  606.  *  promiscuous flag changer v0.1, apk
  607.  *  Linux version
  608.  *
  609.  *  usage: promisc [interface 0|1]
  610.  *
  611.  *  note:  look at README for notes
  612.  */
  613.  
  614. #include <sys/types.h>
  615. #include <sys/socket.h>
  616.  
  617. #include <net/if.h>
  618. #define __KERNEL__
  619. #include <linux/netdevice.h>
  620. #undef __KERNEL__
  621.  
  622. #include <stdio.h>
  623. #include <stdlib.h>
  624. #include <errno.h>
  625. #include <string.h>
  626. #include <fcntl.h>
  627. #include <unistd.h>
  628.  
  629. #define HEAD_NAME "dev_base"
  630. #define PATH_KSYMS "/proc/ksyms"
  631. #define PATH_KMEM "/dev/mem"
  632. #define IFFBITS \
  633. "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING" \
  634. "\10NOARP\11PROMISC\12ALLMULTI\13MASTER\14SLAVE\15MULTICAST"
  635.  
  636. int kread(int fd, u_long addr, void *buf, int len) { 
  637.   int c;
  638.  
  639.   if (lseek(fd, (off_t)addr, SEEK_SET) == -1) 
  640.     return -1;
  641.   if ((c = read(fd, buf, len)) != len) 
  642.     return -1;
  643.   return c;
  644. }
  645.  
  646. int kwrite(int fd, u_long addr, void *buf, int len) { 
  647.   int c;
  648.  
  649.   if (lseek(fd, (off_t)addr, SEEK_SET) == -1) 
  650.     return -1;
  651.   if ((c = write(fd, buf, len)) != len) 
  652.     return -1;
  653.   return c;
  654. }
  655.  
  656. void usage(char *s) {
  657.   printf("usage: %s [interface 0|1]\n", s);
  658.   exit(1);
  659. }
  660.  
  661. main(int argc, char *argv[]) {
  662.   struct device devn, *devp;
  663.   char ifname[IFNAMSIZ];
  664.   int fd, unit, promisc, i, any;
  665.   char *interface, *cp;
  666.   FILE *fp;
  667.   char line[256], symname[256];
  668.  
  669.   switch (argc) {
  670.     case 1:
  671.     promisc = -1;
  672.     interface = NULL;
  673.     break;
  674.     case 3:
  675.         interface = argv[1];
  676.         unit = 0;
  677.         if ((cp = strchr(interface, ':')) != NULL) {
  678.           *cp++ = 0;
  679.           unit = strtol(cp, NULL, 10);
  680.         }
  681.         promisc = atoi(argv[2]);
  682.         break;
  683.     default:
  684.     usage(argv[0]);
  685.   }
  686.  
  687.   if ((fp = fopen(PATH_KSYMS, "r")) == NULL) {
  688.     perror(PATH_KSYMS);
  689.     exit(1);
  690.   }
  691.  
  692.   devp = NULL;
  693.   while (fgets(line, sizeof(line), fp) != NULL && 
  694.                                        sscanf(line, "%x %s", &i, symname) == 2)
  695.     if (strcmp(symname, HEAD_NAME) == 0) {
  696.       devp = (struct device *)i;
  697.       break;
  698.     }
  699.   fclose(fp);
  700.   if (devp ==  NULL) {
  701.     printf("Cannot find symbol: %s\n", HEAD_NAME);
  702.     exit(1);
  703.   }
  704.  
  705.   if ((fd = open(PATH_KMEM, O_RDWR)) == -1) {
  706.     perror(PATH_KMEM);
  707.     exit(1);
  708.   }
  709.   if (kread(fd, (u_long)devp, &devp, sizeof(devp)) == -1) {
  710.     perror("kread");
  711.     exit(1);
  712.   }
  713.  
  714.   for (; devp; devp = devn.next) {
  715.     if (kread(fd, (u_long)devp, &devn, sizeof(devn)) == -1) {
  716.       perror("kread");
  717.       break;
  718.     }
  719.     if (kread(fd, (u_long)devn.name, ifname, sizeof(ifname)) == -1) {
  720.       perror("kread");
  721.       break;
  722.     }
  723.     printf("%s: flags=0x%x ", ifname, devn.flags);
  724.     cp = IFFBITS;
  725.     any = 0;
  726.     putchar('<');
  727.     while ((i = *cp++) != 0) {
  728.       if (devn.flags & (1 << (i-1))) {
  729.     if (any)
  730.       putchar(',');
  731.     any = 1;
  732.     for (; *cp > 32; )
  733.       putchar(*cp++);
  734.       } else 
  735.     for (; *cp > 32; cp++)
  736.       ;
  737.     }
  738.     putchar('>');
  739.     putchar('\n');
  740.     /* This sux */
  741. /*   if (interface && strcmp(interface, ifname) == 0 && unit == ifn.if_unit) {*/
  742.     if (interface && strcmp(interface, ifname) == 0) {
  743.       switch (promisc) {
  744.     case -1: 
  745.         break;
  746.         case 0: if ((devn.flags & IFF_PROMISC) == 0)
  747.           printf("\tIFF_PROMISC not set\n");
  748.                 else {
  749.           printf("\t%s: clearing IFF_PROMISC\n", ifname);
  750.           devn.flags &= ~IFF_PROMISC;
  751.                 if (kwrite(fd, (u_long)devp, &devn, sizeof(devn)) == -1) 
  752.                     break;
  753.                 }
  754.           break;
  755.     default: if ((devn.flags & IFF_PROMISC) == IFF_PROMISC)
  756.           printf("\tIFF_PROMISC set already\n");
  757.                 else {
  758.               printf("\t%s: setting IFF_PROMISC\n", ifname);
  759.           devn.flags |= IFF_PROMISC;
  760.                 if (kwrite(fd, (u_long)devp, &devn, sizeof(devn)) == -1) 
  761.                     break;
  762.                 }
  763.  
  764.       }
  765.     }
  766.   }
  767. }
  768. <-->
  769. <++> EX/promisc/socket-p.c
  770. /*
  771.  *  This is really dumb program.
  772.  *  Works on Linux, FreeBSD and Irix.
  773.  *  Check README for comments.
  774.  */
  775.  
  776. #include <sys/types.h>
  777. #include <sys/socket.h>
  778. #include <sys/time.h>
  779. #include <sys/ioctl.h>
  780. #include <net/if.h>
  781. #include <unistd.h>
  782. #include <stdio.h>
  783. #include <stdlib.h>
  784. #include <string.h>
  785.  
  786. int main(int argc, char *argv[]) {
  787.   int sd;
  788.   struct ifreq ifr;
  789.   char *interface;
  790.   int promisc;
  791.  
  792.   if (argc != 3) {
  793.     printf("usage: %s interface 0|1\n", argv[0]);
  794.     exit(1);
  795.   }
  796.   interface = argv[1];
  797.   promisc = atoi(argv[2]);
  798.  
  799.   if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  800.     perror("socket");
  801.     exit(1);
  802.   }
  803.   strncpy(ifr.ifr_name, interface, IFNAMSIZ);
  804.   if (ioctl(sd, SIOCGIFFLAGS, &ifr) == -1) {
  805.     perror("SIOCGIFFLAGS");
  806.     exit(1);
  807.   }
  808.   printf("flags = 0x%x\n", (u_short)ifr.ifr_flags);
  809.   if (promisc)
  810.     ifr.ifr_flags |= IFF_PROMISC;
  811.   else
  812.     ifr.ifr_flags &= ~IFF_PROMISC;
  813.   if (ioctl(sd, SIOCSIFFLAGS, &ifr) == -1) {
  814.     perror("SIOCSIFFLAGS");
  815.     exit(1);
  816.   }
  817.   close(sd);
  818. }
  819. <-->
  820. <++> EX/promisc/solaris-p.c
  821. /*
  822.  *  promiscuous flag changer v0.1, apk
  823.  *  Solaris version, compile with -lkvm -lelf
  824.  *
  825.  *  usage: promisc [interface 0|1]
  826.  *  (interface has "interface[:<alias number>]" format, e.g. le0:1 or le0)
  827.  * 
  828.  *  note: look at README for notes because DLPI promiscuous request doesn't
  829.  *  set IFF_PROMISC this version is kinda useless. 
  830.  */
  831.  
  832. #include <sys/types.h>
  833. #include <sys/time.h>
  834.  
  835. #include <sys/stream.h>
  836. #include <sys/socket.h>
  837. #include <net/if.h>
  838.  
  839. #define _KERNEL
  840. #include <inet/common.h>
  841. #include <inet/led.h>
  842. #include <inet/ip.h>
  843. #undef _KERNEL
  844.  
  845. #include <kvm.h>
  846. #include <nlist.h>
  847.  
  848. #include <stdio.h>
  849. #include <stdlib.h>
  850. #include <errno.h>
  851. #include <string.h>
  852. #include <fcntl.h>
  853. #include <unistd.h>
  854.  
  855. #define IFFBITS \
  856. "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING" \
  857. "\10NOARP\11PROMISC\12ALLMULTI\13INTELLIGENT\14MULTICAST\15MULTI_BCAST" \
  858. "\16UNNUMBERED\17PRIVATE"
  859.  
  860. struct nlist nl[] = {
  861.     { "ill_g_head" },
  862. #define N_ILL_G_HEAD 0
  863.     { "" }
  864. };
  865.  
  866. int kread(kvm_t *kd, u_long addr, void *buf, int len) {
  867.   int c;
  868.  
  869.   if ((c = kvm_read(kd, addr, buf, len)) != len) 
  870.     return -1;
  871.   return c;
  872. }
  873.  
  874. int kwrite(kvm_t *kd, u_long addr, void *buf, int len) {
  875.   int c;
  876.  
  877.   if ((c = kvm_write(kd, addr, buf, len)) != len) 
  878.     return -1;
  879.   return c;
  880. }
  881.  
  882. void usage(char *s) {
  883.   printf("usage: %s [interface 0|1]\n", s);
  884.   exit(1);
  885. }
  886.  
  887. int main(int argc, char *argv[]) {
  888.   ill_t illn, *illp;
  889.   ipif_t ipifn, *ipifp;
  890.   char ifname[IFNAMSIZ]; /* XXX IFNAMSIZ? */
  891.   int unit, promisc, i, any;
  892.   char *interface, *cp;
  893.   kvm_t *kd;
  894.  
  895.   switch (argc) {
  896.     case 1:
  897.     promisc = -1;
  898.     interface = NULL;
  899.     break;
  900.     case 3:
  901.         interface = argv[1];
  902.         unit = 0;
  903.         if ((cp = strchr(interface, ':')) != NULL) {
  904.           *cp++ = 0;
  905.           unit = strtol(cp, NULL, 10);
  906.         }
  907.     promisc = atoi(argv[2]);
  908.     break;
  909.     default:
  910.     usage(argv[0]);
  911.   }
  912.  
  913.   if ((kd = kvm_open(NULL, NULL, NULL, O_RDWR, argv[0])) == NULL) 
  914.     exit(1);
  915.  
  916.   if (kvm_nlist(kd, nl) == -1) {
  917.     perror("kvm_nlist");
  918.     exit(1);
  919.   }
  920.  
  921.   if (nl[N_ILL_G_HEAD].n_type == 0) {
  922.     printf("Cannot find symbol: %s\n", nl[N_ILL_G_HEAD].n_name);
  923.     exit(1);
  924.   }
  925.  
  926.   if (kread(kd, nl[N_ILL_G_HEAD].n_value, &illp, sizeof(illp)) == -1) {
  927.     perror("kread");
  928.     exit(1);
  929.   }
  930.  
  931.   for (; illp; illp = illn.ill_next) {
  932.     if (kread(kd, (u_long)illp, &illn, sizeof(illn)) == -1) {
  933.       perror("kread");
  934.       break;
  935.     }
  936.     if (kread(kd, (u_long)illn.ill_name, ifname, sizeof(ifname)) == -1) {
  937.       perror("kread");
  938.       break;
  939.     }
  940.     ipifp = illn.ill_ipif;
  941.     /* on Solaris you can set different flags for every alias, so we do */
  942.     for (; ipifp; ipifp = ipifn.ipif_next) {
  943.       if (kread(kd, (u_long)ipifp, &ipifn, sizeof(ipifn)) == -1) {
  944.         perror("kread");
  945.         break;
  946.       }
  947.       printf("%s:%d, flags=0x%x ", ifname, ipifn.ipif_id, ipifn.ipif_flags);
  948.       cp = IFFBITS;
  949.       any = 0;
  950.       putchar('<');
  951.       while ((i = *cp++) != 0) {
  952.         if (ipifn.ipif_flags & (1 << (i-1))) {
  953.           if (any)
  954.         putchar(',');
  955.       any = 1;
  956.       for (; *cp > 32; )
  957.         putchar(*cp++);
  958.         } else 
  959.         for (; *cp > 32; cp++)
  960.         ;
  961.       }
  962.       putchar('>');
  963.       putchar('\n');
  964.       if (interface && strcmp(interface, ifname) == 0 && unit == ipifn.ipif_id){
  965.         switch (promisc) {
  966.           case -1: 
  967.         break;
  968.           case 0: if ((ipifn.ipif_flags & IFF_PROMISC) == 0)
  969.           printf("\tIFF_PROMISC not set\n");
  970.                 else {
  971.           printf("\t%s:%d: clearing IFF_PROMISC\n", ifname, unit);
  972.           ipifn.ipif_flags &= ~IFF_PROMISC;
  973.                 if (kwrite(kd, (u_long)ipifp, &ipifn, sizeof(ipifn)) == -1) 
  974.                     perror("kwrite");
  975.                 }
  976.           break;
  977.       default: if ((ipifn.ipif_flags & IFF_PROMISC) == IFF_PROMISC)
  978.           printf("\tIFF_PROMISC set already\n");
  979.                 else {
  980.               printf("\t%s:%d: setting IFF_PROMISC\n", ifname, unit);
  981.           ipifn.ipif_flags |= IFF_PROMISC;
  982.                 if (kwrite(kd, (u_long)ipifp, &ipifn, sizeof(ipifn)) == -1) 
  983.                     perror("kwrite");
  984.                 }
  985.                 break;
  986.         }
  987.       }
  988.     }
  989.   }
  990. }
  991. <-->
  992.  
  993. ----[  EOF
  994.  
  995.